home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / DVIM72-Mac 1.9.6 / source / string_routines.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-14  |  3.6 KB  |  147 lines  |  [TEXT/R*ch]

  1. #include <ctype.h>
  2. #include "dvihead.h"
  3. #include "commands.h"
  4. #include "gendefs.h"
  5. #include "gblprocs.h"
  6. #include "m72.h"
  7. #include "egblvars.h"
  8.  
  9. /* -*-C-*- strchr.h */
  10. /*-->strchr*/
  11. /**********************************************************************/
  12. /******************************* strchr *******************************/
  13. /**********************************************************************/
  14.  
  15. #ifndef KCC_20
  16. #define KCC_20 0
  17. #endif
  18.  
  19. #ifndef IBM_PC_MICROSOFT
  20. #define IBM_PC_MICROSOFT 0
  21. #endif
  22.  
  23. #ifndef OS_VAXVMS
  24. #define OS_VAXVMS 0
  25. #endif
  26.  
  27.  
  28. #if    (KCC_20 | IBM_PC_MICROSOFT | OS_VAXVMS | OS_THINKC)
  29. #else
  30. char*
  31. strchr(s,c)    /* return address of c in s[], or (char*)NULL if not found. */
  32. register char *s;/* c may be NUL; terminator of s[] is included in search. */
  33. register char c;
  34. {
  35.     while ((*s) && ((*s) != c))
  36.     ++s;
  37.  
  38.     if ((*s) == c)
  39.     return (s);
  40.     else
  41.     return ((char*)NULL);
  42. }
  43. #endif
  44.  
  45. /* -*-C-*- strcm2.h */
  46. /*-->strcm2*/
  47. /**********************************************************************/
  48. /******************************* strcm2 *******************************/
  49. /**********************************************************************/
  50.  
  51.  
  52. /***********************************************************************
  53.  Compare strings (ignoring case), and return:
  54.     s1>s2:    >0
  55.     s1==s2:  0
  56.     s1<s2:    <0
  57. ***********************************************************************/
  58.  
  59. /* toupper() is supposed to work for all letters, but PCC-20 does it
  60. incorrectly if the argument is not already lowercase; this definition
  61. fixes that. */
  62.  
  63. #define UC(c) (islower(c) ? toupper(c) : c)
  64.  
  65. int
  66. strcm2(s1, s2)
  67. register char *s1, *s2;
  68.  
  69. {
  70.     while ((*s1) && (UC(*s1) == UC(*s2)))
  71.     {
  72.     s1++;
  73.     s2++;
  74.     }
  75.     return((int)(UC(*s1) - UC(*s2)));
  76. }
  77. #undef UC
  78.  
  79. /* -*-C-*- strid2.h */
  80. /*-->strid2*/
  81. /**********************************************************************/
  82. /******************************* strid2 *******************************/
  83. /**********************************************************************/
  84.  
  85. /* toupper() is supposed to work for all letters, but PCC-20 does it
  86. incorrectly if the argument is not already lowercase; this definition
  87. fixes that. */
  88.  
  89. #define UC(c) (islower(c) ? toupper(c) : c)
  90.  
  91. int
  92. strid2(string,substring)/* Return index (0,1,...) of substring in string */
  93. char string[];        /* or -1 if not found.  Letter case is IGNORED. */
  94. char substring[];
  95. {
  96.     register int k;    /* loop index */
  97.     register int limit;    /* loop limit */
  98.     register char *s;
  99.     register char *sub;
  100.  
  101.     limit = (int)strlen(string) - (int)strlen(substring);
  102.  
  103.     for (k = 0; k <= limit; ++k)/* simple (and slow) linear search */
  104.     {
  105.     s = &string[k];
  106.  
  107.     for (sub = &substring[0]; (UC(*s) == UC(*sub)) && (*sub); (++s, ++sub))
  108.         /* NO-OP */ ;
  109.  
  110.     if (*sub == '\0')    /* then all characters match */
  111.         return(k);        /* success -- match at index k */
  112.     }
  113.  
  114.     return(-1);            /* failure */
  115. }
  116.  
  117. /* -*-C-*- strrchr.h */
  118. /*-->strrchr*/
  119. /**********************************************************************/
  120. /****************************** strrchr *******************************/
  121. /**********************************************************************/
  122.  
  123. #if    (KCC_20 | IBM_PC_MICROSOFT | OS_VAXVMS | OS_THINKC)
  124. #else
  125. char*
  126. strrchr(s,c)    /* return address of last occurrence of c in s[], */
  127.         /* or (char*)NULL if not found.  c may be NUL; */
  128.         /* terminator of s[] is included in search. */
  129. register char *s;
  130. register char c;
  131. {
  132.     register char *t;
  133.  
  134.     t = (char *)NULL;
  135.     for (;;)        /* loop forever */
  136.     {
  137.     if (*s == c)
  138.         t = s;    /* remember last match position */
  139.     if (!*s)
  140.         break;    /* exit when NULL reached */
  141.     ++s;        /* advance to next character */
  142.     }
  143.     return (t);
  144. }
  145. #endif
  146.  
  147.